package com.ms.architecture.utils

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.WindowManager

object ScanUtil {

    private const val PDA_FILTER = "scan.action.my.scanner"
    private const val OUTPUT_MODE_ACTION = "action.bar.scancfg"
    private const val OUTPUT_MODE_VALUE = "EXTRA_SCAN_MODE"
    private const val TRIG_MODE_VALUE = "EXTRA_TRIG_MODE"
    private const val SCAN_ENCODE_VALUE = "SCAN_ENCODE"
    private const val NON_REPEAT_VALUE = "NON_REPEAT_TIMEOUT"//不发送重复条码数据的时间间隔（毫秒）
    private const val FILL = 1 //直接填充
    private const val VIRTUAL_KEY = 2 //模拟键
    private const val BROADCAST = 3 //广播

    private const val BARCODE1 = "SCAN_BARCODE1"
    private const val BARCODE2 = "SCAN_BARCODE2"
    private const val BARCODE_TYPE = "SCAN_BARCODE_TYPE"
    private const val SCAN_STATUS = "SCAN_STATE"

    fun registerScanBroadcastReceiver(context: Context, receiver: BroadcastReceiver?) {
        val filter = IntentFilter()
//        val intent = Intent(OUTPUT_MODE_ACTION)
        filter.addAction(OUTPUT_MODE_ACTION)
        context.registerReceiver(receiver, filter)
//        setScanType(context, BROADCAST)
//        setScanMode(context)
//        (context as Activity).window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    fun unRegisterScanBroadcastReceiver(context: Context, receiver: BroadcastReceiver?) {
        context.unregisterReceiver(receiver)
        (context as Activity).window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    fun setScanType(context: Context, type: Int) {
        val intent = Intent(OUTPUT_MODE_ACTION)
        intent.putExtra(OUTPUT_MODE_VALUE, type)
        context.sendBroadcast(intent)
    }

//    fun setScanMode(context: Context) {
//        val intent = Intent(OUTPUT_MODE_ACTION)
//        intent.putExtra(TRIG_MODE_VALUE, 0)
//        intent.putExtra(SCAN_ENCODE_VALUE, 1)//编码格式
//        intent.putExtra(NON_REPEAT_VALUE, 1500)//编码格式
//        context.sendBroadcast(intent)
//    }

    fun getBarCode1(intent: Intent): String? {
        return if ("ok" == intent.getStringExtra(SCAN_STATUS)) intent.getStringExtra(
            BARCODE1
        )?.trim() else ""
    }

    fun getBarCode2(intent: Intent): String? {
        return if ("ok" == intent.getStringExtra(SCAN_STATUS)) intent.getStringExtra(
            BARCODE2
        )?.trim() else ""
    }

    fun getRealCode(intent: Intent): String? {
        val code = getBarCode1(intent) ?: ""
        if (code.contains("?")) {
            val codeSplit = code.split("?")
            if (codeSplit.size != 2) {
                return ""
            }
            var realCode = codeSplit[1]
            if (realCode.length < 12) {
                return ""
            }
            if (realCode.length == 27 && realCode.startsWith("3")) {
                realCode = realCode.substring(1, realCode.length - 2)
            }
            return realCode
        }
        return code
    }

    fun getValue(intent: Intent, key: String): String? {
        return intent.extras?.getString("com.symbol.datawedge.data_string")
    }

}